﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using Grishko.DataTier.SqlSentence;
namespace Grishko.UI
{   
    public class FilterPanelDesigner : ControlDesigner
    {
      

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
           
        }

      
        public override string GetDesignTimeHtml()
        {
            
            //return configured html
            string designTimeHtml = "Панель фильтра (симпатишная наборная)";
            
            return designTimeHtml;
        }

        protected override string GetErrorDesignTimeHtml(Exception e)
        {

            return CreatePlaceHolderDesignTimeHtml("There was an error rendering the TemplateMenu control." +
               "<br>Exception: " + e.Source + "  Message: " + e.Message);
        }

        
    }

    public class BasicTemplateContainer : WebControl, INamingContainer
    {
        public BasicTemplateContainer()
            : base(HtmlTextWriterTag.Span)
        {
            this.BorderWidth = 0;
            this.BorderStyle = BorderStyle.Outset;
        }
    }


    /// <summary>
    ///  Позволяет скомбинировать два контрола - FilterPanel и FilterEditor
    /// </summary>
    [ToolboxData("<{0}:FilterPanelFrame runat=server></{0}:FilterPanelFrame>"),
     Designer(typeof(FilterPanelDesigner))]
    public class FilterPanelFrame : CompositeControl
    {

        public FilterPanelFrame()
            : base()
        {

        }


        private ITemplate _BodyTemplate1;
        [Browsable(false), Description("BodyTemplate1"), PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(BasicTemplateContainer))]
        public ITemplate BodyTemplateItem1
        {
            get
            {
                return _BodyTemplate1;
            }
            set
            {
                _BodyTemplate1 = value;
            }
        }

        private ITemplate _BodyTemplate2;
        [Browsable(false), Description("BodyTemplate2"), PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(BasicTemplateContainer))]
        public ITemplate BodyTemplateItem2
        {
            get
            {
                return _BodyTemplate2;
            }
            set
            {
                _BodyTemplate2 = value;
            }
        }

        private ITemplate _BodyTemplate3;
        [Browsable(false), Description("BodyTemplate3"), PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(BasicTemplateContainer))]
        public ITemplate BodyTemplateItem3
        {
            get
            {
                return _BodyTemplate3;
            }
            set
            {
                _BodyTemplate3 = value;
            }
        }

        private ITemplate _ScritTemplate;
        [Browsable(false), Description("юбка фильтра"), PersistenceMode(PersistenceMode.InnerProperty),
        TemplateContainer(typeof(BasicTemplateContainer))]
        public ITemplate ScritTemplate
        {
            get
            {
                return _ScritTemplate;
            }
            set
            {
                _ScritTemplate = value;
            }
        }

        private void CreateControlHierarchy()
        {
            IFilterEditor FilterEditor = null;
            IFilterPanel FilterBody = null;
            BasicTemplateContainer Body = new BasicTemplateContainer();

            switch (ActiveBodyFilterTemplate)
            {
                case 1:
                    BodyTemplateItem1.InstantiateIn(Body);
                    break;
                case 2:
                    BodyTemplateItem2.InstantiateIn(Body);
                    break;
                case 3:
                    BodyTemplateItem2.InstantiateIn(Body);
                    break;
                default :
                    break;
            }
            FilterBody = (IFilterPanel)Body.FindControl("ctlFilterPanel");
            Controls.Add(Body);
            //FilterBody.OnHideShowEditor = SetShowHideScrit;

            // прорисовывать редактор 
            if (!ShowScrit)
                    return;

            if (ScritTemplate  != null)
            {
                BasicTemplateContainer Scrit = new BasicTemplateContainer();
                ScritTemplate.InstantiateIn(Scrit);
                FilterEditor = (IFilterEditor)Scrit.FindControl("ctlFilterEditor");
                Controls.Add(Scrit);
                // сцепляем 
                //FilterEditor.FilterScriptEngine = FilterBody.FilterScriptEngine;
            }

        }
      
        private bool SetShowHideScrit(bool value)
        {
            ShowScrit=value;
            DataBind();
            return true;
        }

        private bool ShowScrit
        {
            get
            {
                if (null == ViewState["ShowEditorKey"]) return false;
                return Convert.ToBoolean(ViewState["ShowEditorKey"]);
            }
            set { ViewState["ShowEditorKey"] = value.ToString(); }
        }

        public  int ActiveBodyFilterTemplate
        {
            get
            {
                if (null == ViewState["ActiveBodyFilterTemplate"]) return 1;
                return Convert.ToInt16 (ViewState["ActiveBodyFilterTemplate"]);
            }
            set { ViewState["ActiveBodyFilterTemplate"] = value.ToString(); }
        }


        override protected void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();            
        }

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

        public override void DataBind()
        {
            CreateChildControls();
            ChildControlsCreated = true;

            base.DataBind();
        }


    }
}